<!-- iCheck -->
<script src="https://cdn.jsdelivr.net/npm/icheck@1.0.2/icheck.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/icheck@1.0.2/skins/all.css">
<style>
	.order-lists{ max-height: 200px; overflow: hidden;}
	.active th{ font-size: 13px; }
</style>
<!-- ./ iCheck -->
<!-- 按订单状态查看 -->
<div class="container padding page-content">
	<div class="row">
    	<div class="col-md-2">
			{include file="common/user-nav"}
		</div>

		<div class="col-sm-10">

			<!-- 面包屑路径导航 -->
				<ol class="breadcrumb hidden-xs">
					<li><a href="{:addon_url('leescore/index/index')}">{:__('store title')}</a></li>
					<li class="active">{:__('order list')}</li>
				</ol>
			<!-- 面包屑路径导航 -->
			<div class="goods-right">
				<!-- 开启了订单清理 -->
				{if($config['open_clear'] == 1)}
					<div class="alert alert-danger radius-none">
						{:__('pay timeout tips', array($config['order_out_time']))}
					</div>
				{/if}
				<div class="box radius-none">
					<div class="box-header">
						<h3 class="box-title hidden-xs">{:__('order list')}</h3>
						<div class="visible-xs"><br /></div>
						<div class="box-tools">
							<form action="{:addon_url('leescore/order/index',array('listType' => input('get.listType')))}" method="POST" name="Search" class="form-inline">
								<div class="input-group input-group-sm" style="width: 250px;">
									<input type="text" value="{$keywords}" name="keywords" class="form-control pull-right radius-none" placeholder="{:__('Please enter the order number')}">
									<input type="hidden" name="status" value="{:input('get.listType')}" />
									<div class="input-group-btn">
										<button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
									</div>
								</div>
							</form>
						</div>
					</div>
					<div class="box-body table-responsive no-padding">
						<table class="table table-responsive table-bordered no-margin">
							<tbody>
								<tr class="active">
									<th><input type="checkbox" name="all" class="all checkbox" /></th>
									<th>{:__('order_id')}</th>
									<th style="width: 180px">{:__('List of goods')}</th>
									<th style="width: 120px">{:__('total')}</th>
									<th style="width: 80px">{:__('order status')}</th>
									<th>{:__("create time")}</th>
									<th style="width: 170px;">{:__('action')} <a href="javascript:;" class="btn btn-xs btn-danger del-selected pull-right btn-flat">{:__('delete selected')}</a></th>
								</tr>
								{if($list->isEmpty())}
									<tr>
										<td colspan="7">{:__('empty')}</td>
									</tr>
								{/if}
								{volist name="list" id="vo"}
									<tr>
										<td><input type="checkbox" name="ids[]" value="{$vo.id}" class="checkbox ids"></td>
										<td style="width: 100px;">{$vo.order_id}</td>
										<td style="width: 30%">
											<div class="order-lists" style="position:relative">
												{volist name="$vo.order_goods" id="v"}
												<div class="margin-top-small">
													<div class="col-sm-12 row">
														<a href="{:addon_url('leescore/goods/details',array('gid' => $v.goods_id))}">{$v.goods_name}</a>
													</div>
													<div class="col-sm-6 padding-none">
														<ul>
															<li class="pull-left padding-min"><a href="{:addon_url('leescore/goods/details',array('gid' => $v.goods_id))}"><img src="{$v.goods_thumb}" class="img-responsive" style="height: 40px; width: 40px; border-radius: 3px;" /></a></li>
														</ul>
													</div>
													<div class="col-sm-3 padding-none">
														<span class="text-warning" title="{:__('score')}"><i class="glyphicon glyphicon-piggy-bank"></i> {$v.score|default='0'}</span>
														<br>
														<span class="text-danger" title="{:__('price')}"><i class="glyphicon glyphicon-yen"></i> {$v.money|default="0"}{:__("money")}</span>
													</div>
													<div class="col-sm-3">
														* <span class="text-danger">{$v.buy_num}</span>
													</div>
													<div class="clearfix"></div>
												</div>
												<?php
													$more = '';
													if($i >= 3)
													{
														$more = '...';
														break;
													}
												?>
												{/volist}
												<div class="clearfix text-center">{$more}</div>
											</div>
										</td>
										<td>
											<span class="glyphicon glyphicon-piggy-bank text-warning" title="{:__('score')}"> {$vo.score_total|default='0'}</span>
											<br>
											<span class="glyphicon glyphicon-yen text-danger" title="{:__('price')}"> {$vo.money_total|default="0"}{:__("money")}</span>
										</td>
										<td>
											{switch name="vo.status"}
												{case value="-2"} <span class="label label-danger">{:__('admin faild')}</span> {/case}
												{case value="0"} <span class="label label-default">{:__('unpaid')}</span> {/case}
												{case value="1"} <span class="label label-success">{:__('paid')}</span> {/case}
												{case value="2"} <span class="label label-info">{:__('shipped')}</span> {/case}
												{case value="3"} <span class="label label-primary">{:__('sign for')}</span> {/case}
												{case value="4"} <span class="label label-warning">{:__('outing')}</span> {/case}
												{case value="5"} <span class="label label-danger">{:__('out success')}</span> {/case}
												{default /} {:__('order error')}
											{/switch}

										</td>
										<td>{$vo.createtime|date="Y-m-d H:i",###}</td>
										<td>
											{if($vo['status'] == '0')}
												<a href="{:addon_url('leescore/order/postOrders',array('orderid' => $vo.id))}"  target="_blank" class="btn btn-warning btn-xs order-gotopay">{:__('order pay')}</a>
											{/if}
											<a  href="javascript:;" data-id="{$vo.id}" data-status="{$vo.status}" class="btn btn-default btn-xs show-details">
												{:__('detail')}
											</a>
											{in name="$vo.status" value="3,5,-2"}
												<a href="javascript:;" data-param="{$vo.id}" class="btn btn-danger btn-xs order-delete">{:__('delete order')}</a>
											{/in}
											{in name="$vo.status" value="2"}
												<a href="javascript:void(0);" data-param="{$vo.id}" class="btn btn-warning btn-xs order-sign">{:__('order sign')}</a>
											{/in}
											{in name="$vo.status" value="0"}
											<a href="javascript:;" data-param="{$vo.id}" data-goodsid="{$vo.id}" class="btn btn-danger btn-xs close-order">{:__('close order')}</a>
											{/in}
										</td>
									</tr>
								{/volist}
							</tbody>
						</table>
					</div>
					<!-- /.box-body -->
					{if($list->render() != null)}
						<div class="box-footer clearfix text-center">
							{$list->render()}
						</div>
					{/if}
				</div>

				<div class="goods-list">

				</div>
				<div class="col-sm-12 padding text-center page">
					
				</div>
			</div>
		</div>
	</div>
</div>

<!-- Layer -->
<script src="__CDN__/assets/libs/fastadmin-layer/dist/layer.js"></script>
<script>
	$(document).ready(function() {
		$('input').iCheck({
			checkboxClass: 'icheckbox_minimal-orange',
			radioClass: 'iradio_minimal',
			increaseArea: '10%' // optional
		});

		//全选
		$('input.all').on('ifChecked', function(){
			$('input.ids').iCheck('check');
		});

		//取消全选
		$('input.all').on('ifUnchecked', function(){
			$('input.ids').iCheck('uncheck');
		});

		//删除订单
		$(".order-delete").on('click', function() {
			var id = $(this).attr("data-param");
			$.ajax({
				url: '{:addon_url("leescore/order/delone")}',
				type: 'get',
				dataType: 'json',
				data: {id: id},

				success:  function (json)
				{
					if(json.status == true)
					{
						layer.msg('{:__("delete success")}',{
							icon: 6
						});
						setTimeout(function() {
							window.location.reload();
						}, 2000);
					}else{
						layer.msg('{:__("delete faild param error")}',{
							icon: 2
						});
					}
				}
			});
		});

		//关闭订单
		$(".close-order").on('click', function() {
			var id = $(this).attr("data-param");
			layer.confirm("订单关闭后将不可恢复, 是否继续关闭订单？",{
				title: "{:__('close title')}",
				btn: ["{:__('close')}","{:__('cannel')}"],
			},function(){
				$.ajax({
					url: '{:addon_url("leescore/order/closeOrder")}',
					type: 'get',
					dataType: 'json',
					data: {id: id},
					success: function (json)
					{
						//console.log(json);
						if(json.status == true)
						{
							layer.msg('{:__("order close success")}',{
								icon: 6
							});
							setTimeout(function() {
								window.location.reload();
							}, 2000);
						}else{
							layer.msg('{:__("order close faild param error")}',{
								icon: 2
							});
							console.log(json.msg);
						}
					}
				});
			},function(){
				layer.close();
			});
		});

		//删除选中订单
		$(".del-selected").on('click', function() {
			var empty = true;

			//检查选中状态
			$(".ids").each(function(index, el) {
				if($(this).prop("checked") == true)
				{
					empty = false;
				}
			});

			//没有被选中的订单
			if(empty == true){
				layer.msg('{:__("please selected order")}',{icon: 7});
				return false;
			} 

			//序列化选中ID
			var ids = $(".ids").serialize();
			layer.confirm("订单删除后将不可恢复, 您是否要继续删除选中的订单？", 
				{
					title: "删除询问",
					btn: ['是的, 我要继续删除','不了, 我点错的'] //按钮
				}, 
				function()
				{
					layer.msg('123');
					$.ajax({
						url: '{:addon_url("leescore/order/delete")}'+"?"+ids,
						type: 'post',
						dataType: 'json',
						data: {data: ids},

						success: function (json)
						{
							if(json.status == true)
							{
								layer.msg('{:__("delete success")}', {
									icon: 6
								});
								setTimeout(function(){
									window.location.reload();
								}, 2000);
							}else{
								layer.msg('{:__("delete faild param error")}', {
									icon: 2
								});
							}
						}
					}).fail(function() {
						console.log("Ajax请求发生错误");
					});
				}, 
				function()
				{
					layer.close();
				});			
			return false;
		});

		//内容详情
		$('.show-details').on('click', function() {
			var bwith = $("body").width();
    		var id = $(this).attr('data-id');
    		var btnurl = "{:addon_url('leescore/order/postOrders')}";
			var btnurl = btnurl + "?orderid=" + id;
			var status = $(this).attr('data-status');
    		var url = '{:addon_url("leescore/order/details")}';
    		url = url + "?id=" + id;
    		var w = bwith > 768 ? '75%' : '100%';
    		var btn = status == 0 ? ['{:__("go to pay")}','{:__("close")}'] : '';
			layer.open({
				type: 2,
				title: '{:__("order detail")}',
				shadeClose: true,
				shade: 0.7,
				closeBtn:2,
				area: [w, w],
				closeBtn: false,
				btn: btn,
				yes: function(index, layero){
					if(btnurl != false)
					{
						window.location.href = btnurl;
					}else{
						layer.close(index);
					}
				},function(){
					layer.close(index);
				},

				content: url //iframe的url
			});
		});

		/*条件菜单宽度计算*/

			// body宽度
    		var bodyw = $("body").width();
    		var wsum = 0;
    		$(".order-nav li").each(function(index, el) {
    			//容器宽度+左右内间距
    			var w = $(this).width() + 20;
    			wsum = wsum + w;
    		});

    		// 转换成百分比
    		var odd = (wsum / bodyw) > 1 ? (wsum / bodyw) * 100 : 1 * 100;

    		// 设置容器宽度
    		$(".order-nav").width(odd+"%");
    		$(".order-nav").parent("div.text-muted").width(odd+"%");

		/*条件菜单宽度计算*/


		//删除选中订单
		$(document).on('click', 'a.order-delete', function() {
			var id = $(this).attr('data-param');
			var btn = $(this);
			layer.confirm('{:__("delete order tip")}', {
				icon: 2,
				title: '{:__("action tips")}',
				btn: ['{:__("yes")}','{:__("no")}'] //按钮
			}, function(){
				var uri = '{:addon_url("leescore/order/delete")}';
				$.ajax({
					url: uri,
					type: 'post',
					dataType: 'json',
					data: {id: id},

					success: function(result){
						if(result != true)
						{
							console.log(result);
						}else
						{
							btn.parent("td").parent("tr").remove();
							layer.msg('{:__("action success")}');
						}
					}
				});
				
			}, function(){
				layer.close();
			});
		});

		//确认收货
		$(".order-sign").on('click', function() {
			var id = $(this).attr('data-param');
			var btn = $(this);
			layer.confirm('{:__("order sign tip")}', {
				icon: 3,
				title: '{:__("action tips")}',
				btn: ['{:__("yes")}','{:__("no")}'] //按钮
			}, function(){
				var uri = '{:addon_url("leescore/order/orderSign")}';
				$.ajax({
					url: uri,
					type: 'post',
					dataType: 'json',
					data: {id: id},

					success: function(result){
						if(result != true)
						{
							console.log(result);
						}else
						{
							btn.parent("td").prev("td").prev("td").html("<span class=\"label label-primary\">{:__('sign for')}</span>");

							btn.parent("td").append("<a href=\"javascript:void(0);\" data-param=\""+ id +"\" class=\"btn btn-danger btn-xs order-delete\">{:__('delete order')}</a>").children('a.order-sign').remove();
							layer.msg('{:__("action success")}');
						}
					}
				});
				
			}, function(){
				layer.close();
			});
		});
	});
</script>
